package vn.appsmobi.ulti;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.Toast;

public class DownloadCompleteReceiver extends BroadcastReceiver
{
  public void onReceive(Context context, Intent intent)
  {
	  Log.v("Receive Download complete");
    if ("android.intent.action.DOWNLOAD_COMPLETE".equals(intent.getAction()))
    {
      
      long downloadid = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);      
      //
      
      
      Query query = new Query(); 
      query.setFilterById(downloadid); 
      Cursor c = DownloadInstallManager.getManager().mDownloadManager.query(query); 
      if (c.moveToFirst()) { 
          int columnIndex = c 
                  .getColumnIndex(DownloadManager.COLUMN_STATUS); 
          
          Log.v("column index :" + c.getInt(columnIndex));
          
          if (DownloadManager.STATUS_SUCCESSFUL == c 
                  .getInt(columnIndex)) { 
        	  DownloadInstallManager.getManager().handleDownloadComplete(downloadid);

          } else if(DownloadManager.ERROR_FILE_ALREADY_EXISTS == c.getInt(columnIndex)){ 
        	  Log.v("Receive Download complete: file already exists");
          } 
      }
    }
  }
}

